38. Enforcing script prerequisites¶
Note
The below information is extensively based in information taken from the PowerShell® Notes for Professionals book. I plan to extend this information based on my day to day usage of the language.
38.1: Enforce minimum version of PowerShell host¶
1 | #requires -version 4
|
After trying to run this script in lower version, you will see this error message
1 | .\script.ps1 : The script 'script.ps1' cannot be run because it contained a "#requires" statement at line 1 for Windows PowerShell version 5.0. The version required by the script does not match the currently running version of Windows PowerShell version 2.0. |
38.2: Enforce running the script as administrator¶
Version ≥ 4.0
1 | #requires -RunAsAdministrator
|
After trying to run this script without admin privileges, you will see this error message
1 | .\script.ps1 : The script 'script.ps1' cannot be run because it contains a "#requires" statement for running as Administrator. The current Windows PowerShell session is not running as Administrator. Start Windows PowerShell by using the Run as Administrator option, and then try running the script again. |